<template>
<!--  <h2>密级鉴定</h2>-->
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <fileTrees @handleNodeClick="onTreeClick" :tableHeight="treeHeight" :isnoClick="isnoClick" :menuId="menuId"></fileTrees>
      </el-col>

      <el-col :span="20" :xs="24" v-show="isShow">
        <queryCriteriaList v-if="listFrom.length > 0" :showSearch="showSearch" :listFrom="listFrom"
                           @resetQuery="resetQuery" @handleQuery="handleQuery" ref="queryForm"></queryCriteriaList>

        <el-row :gutter="10" class="mb8" ref="refbut">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-set-up"  @click="adjust" :disabled="isAdjust" v-hasPermi="['archive:mjidentify:cancelApproval']">调整密级</el-button>
          </el-col>
        </el-row>

        <tableList v-if="tableHeight" :tableHeaderList="tableHeaderList" ref="ref"
                   @handleSelectionChange="handleSelectionChange" :tableData="tableData" :loading="loading"
                   :tableHeight="tableHeight" @celldblclick="celldblclick" @sortChange="sortChange"></tableList>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>

      <el-col :span="20" :xs="24" v-show="!isShow" class="empty">
        <el-empty :image-size="200"></el-empty>
      </el-col>
    </el-row>

    <!-- 调整密级 -->
    <el-dialog title="调整密级" :visible.sync="open" width="25%" style="margin-top:8%;" append-to-body :before-close="closeDelDialog"    v-hasPermi="['archive:mjidentify:cancelApproval']" >
      <el-form ref="mjForm" :model="mjForm" :rules="rules" label-width="80px">
        <el-form-item label="密级" prop="value">
          <el-select v-model="mjForm.value" placeholder="请选择密级">
            <el-option v-for="dict in renyuanOptions" :key="dict.dictValue" :value="dict.dictValue" :label="dict.dictLabel"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>

    <!--start  组件详情 -->
    <jianDetailsOpen v-if="jianDetailsOpen" :open="jianDetailsOpen"
                     :celldbData="celldbData" @detailsHandleClose="detailsHandleClose">
    </jianDetailsOpen>
    <!--end 组件详情 -->
    <!-- start 组卷\项目详情 -->
    <juanDetailsOpen v-if="juanDetailsOpen" :open="juanDetailsOpen" :celldbData="celldbData"
                     @detailsHandleClose="detailsHandleClose"></juanDetailsOpen>
    <!-- end 组卷\项目详情 -->
  </div>
</template>

<script>
import fileTrees from "@/components/fileTrees/index.vue"  //档案树
import treeHeight from '@/mixins/treeHeight'
import queryCriteriaList from "@/components/queryCriteriaList/index.vue"  //查询列表
import tableList from "@/components/tableList/index.vue"
import jianDetailsOpen from '@/components/componentsJianDetails/index.vue' //件详情
import juanDetailsOpen from '@/components/componentsJuanDetails/index.vue' //卷详情
import {getGroupTree} from "@/api/archive/archivesCategory";
import { getDicts_archive } from "@/api/archive/dict/dictManageData";
import {
    getdamjbyrymj
} from "@/api/archive/fileCollection/offlinReception.js";
import {
  approval, getCategoryName,
  getqueryfieldList,
  getTableList,
  getthbytreenodeHeaderList
} from "@/api/archive/archivalAppraisal/classification/identify";
import {getLastIndexOf} from "@/utils";
//计算左侧树的高度
export default {
  name: "index",
  mixins: [treeHeight], // 混入方法
  components: { fileTrees, queryCriteriaList, tableList, jianDetailsOpen, juanDetailsOpen },
  archive_Dicts: ['MJ'],
  data() {
    return {
      menuId: '',
      isnoClick: true, //防止树重复点击
      isShow: false, //是否显示右侧数据
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      listFrom: [], //查询框列表
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableHeight: "",
      tableHeaderList: [], //table表头数据
      tableData: [],//table列表数据
      subStr: "",  //获取点击当前树的 件 卷  项目 截取 sub_table_name显示
      jianDetailsOpen: false,//件详情弹窗
      juanDetailsOpen: false,//卷详情弹窗
      detail: [],
      mj: [],
      open: false,
      mjForm: {
        value: undefined
      },
      rules: {
        value: [
          { required: true, message: "请输入密级", trigger: "blur" },
        ]
      },
      // 防止重复点击
      noClick: true,
      // 项目层禁用调整密级
      isAdjust: true
    }
  },
  created() {
    this.menuId = this.$route.meta.menuId
    getDicts_archive("MJ").then(res => { //人员密级
            getdamjbyrymj().then(resdata => { //此接口没有返回dictLabel，需要组装接口
                var mjdata = [];
                for (var i in resdata.data) {
                    for (var j in res.data) {
                        //  console.log(resdata.data[i].daMj, res.data.MJ[j].dictValue)
                        if (resdata.data[i].daMj == res.data[j].dictValue) {
                            mjdata.push(res.data[j])
                        }
                    }
                }

                this.renyuanOptions = mjdata

            })
        })
  },
  methods: {
    //动态设置table的高度
    tableHeightData() {
      this.$nextTick(
        () => {
          let h = document.documentElement.clientHeight
          h = h - 200
          if (this.$refs.queryForm) { // 页面上如果有面包屑区域徐减去它的高度
            h = h - this.$refs.queryForm.$el.offsetHeight
          }

          if (this.$refs.refbut) { // 页面上如果有分页区域徐减去它的高度
            h = h - this.$refs.refbut.$el.offsetHeight
          }
          this.tableHeight = h > 310 ? h : 310 // 根据自己项目定高度
          return h
        }
      )
    },
    // 树节点单击事件
    onTreeClick(data) {
      console.log(data)
      this.isnoClick = false;
      if(data.categoryId!=null){
        this.isShow = true
      } else {
        this.isShow = false
      }
      if (data.nodeType == null || data.nodeType == "t" || data.nodeType == "f") {
        this.isnoClick = true;
        return false;
      }
      this.queryParams = data;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.menuId = this.$route.meta.menuId
      if (data.sub_table_name == null) {
        this.getGroupTreePost(data.categoryId);//档案门类树门类根据categoryID回显
      } else {
        this.queryParams.sub_table_name = data.sub_table_name;
        this.subStr = getLastIndexOf(this.queryParams.sub_table_name);
        if(this.subStr == 'F') {
          this.queryParams.type = '0'
          this.isAdjust = false
        } else if (this.subStr == 'V') {
          this.queryParams.type = '1'
          this.isAdjust = false
        } else if (this.subStr == 'X') {
          this.queryParams.type = '3'
          this.isAdjust = true
        }
        this.getqueryfieldListPost();//点击树节点获取查询字段
        this.getlistHeaderPost();/**点击树节点,获取table表头数据 */
      }
    },
    //档案门类树门类根据categoryID回显
    getGroupTreePost(ID) {
      getGroupTree(ID).then(response => {
        this.responselist=response.data;
        if (response.data.type == "0") { //件
          this.queryParams.sub_table_name = response.data.fileTableName;
        } else if (response.data.type == "1") { //卷
          this.queryParams.sub_table_name = response.data.folderTableName;
        } else { //项目
          this.queryParams.sub_table_name = response.data.itemTableName;
        }
        this.subStr = getLastIndexOf(this.queryParams.sub_table_name);
        if(this.subStr == 'F') {
          this.queryParams.type = '0'
          this.isAdjust = false
        } else if (this.subStr == 'V') {
          this.queryParams.type = '1'
          this.isAdjust = false
        } else if (this.subStr == 'X') {
          this.queryParams.type = '3'
          this.isAdjust = true
        }
        this.getqueryfieldListPost();//点击树节点获取查询字段
        this.getlistHeaderPost();/**点击树节点,获取table表头数据 */
      });
    },
    /** 点击树节点获取查询字段 */
    getqueryfieldListPost(data) {
      this.queryParams.searchcolumn = undefined
      this.listFrom = []
      getqueryfieldList({
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        subTableName: this.queryParams.sub_table_name,
      }).then(response => {
        this.listFrom = response.rows;
      });
    },
    /**点击树节点,获取table表头数据 */
    getlistHeaderPost() {
      getthbytreenodeHeaderList({
          categoryId: this.queryParams.categoryId, menuId: this.queryParams.menuId, subTableName: this.queryParams.sub_table_name
        }
      ).then(response => {
        this.tableHeaderList = response.rows;
        var fields = []
        for (var i in response.rows) {
          fields.push(response.rows[i].FIELD_NAME)
        }
        this.queryParams.fields = fields.toString()
        this.getList();
      });
    },
    //表单重置
    resetQuery() {
      this.queryParams.searchcolumn = undefined
      this.queryParams.order = undefined;
      this.$refs.ref.clearSort()
      this.getList();
    },
    //点击搜索
    handleQuery(searchcolumn) {
      var column = searchcolumn.filter(item => { return item.value !== '' })
      this.queryParams.searchcolumn = column
      this.queryParams.order = undefined;
      this.$refs.ref.clearSort()
      this.getList();
    },
    getList() {
      getTableList(this.queryParams).then(response=>{
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
        this.isnoClick=true;
        this.tableHeightData();
      })
    },
    //是否选择数据
    handleSelectionChange(ids, single, multiple,selection) {
      this.ids = ids.toString();
      this.single = single;
      this.multiple = multiple;
      this.detail = selection.map(item => {
        return {
          filesId: item.ID,
          title: item.TITLE,
          mj: this.getMj(item.MJ)
        }
      })
      this.mj = selection.map(item => item.MJ)
    },
    getType() {
      var str = getLastIndexOf(this.queryParams.sub_table_name);
      var type;
      if(str == 'F') {
        type = '0'
      } else if (str == 'V') {
        type = '1'
      } else if (str == 'X') {
        type = '3'
      }
      return type
    },
    getMj(mj) {
      if(mj == undefined || mj == null || mj == ''){
        return ''
      } else {
        for (let i = 0; i < this.dict.type.MJ.length; i++) {
          if (mj == this.dict.type.MJ[i].value) {
            return this.dict.type.MJ[i].label
          }
        }
      }
    },
    //排序
    sortChange(val) {
      // console.log(val, "ksdfjkl")
      this.queryParams.order = val;
      // console.log(this.queryParams, "+++")
      this.getList();
    },
    // 调整密级
    adjust() {
      if(this.ids != undefined&&!this.multiple) {
        const isEqual = this.mj.every((value, index, array) => value === array[0]);
        if(isEqual) {
          this.open = true
          this.$set(this.mjForm, 'value', this.mj[0])
        } else {
          this.$message({ message: '密级状态不一致', type: 'warning' })
        }
      } else {
        this.$message({ message: '请选择至少一条数据进行调整', type: 'warning' })
      }
    },
    submitForm() {
      this.$refs["mjForm"].validate(valid => {
        if(valid) {
          if(this.noClick) {
            this.noClick = false
            var mj;
            for (let i = 0; i < this.dict.type.MJ.length; i++) {
              if(this.mjForm.value == this.dict.type.MJ[i].value) {
                mj = this.dict.type.MJ[i].label
              }
            }
            console.log(this.detail)
            getCategoryName(this.queryParams.categoryId).then(response=>{
              var categoryName = response.data.name
              this.form = this.detail.map(item => {
                return {
                  subTableName: this.queryParams.sub_table_name,
                  mjType: this.mjForm.value,
                  categoryId: this.queryParams.categoryId,
                  categoryName: categoryName,
                  menuId: this.queryParams.menuId,
                  type: this.queryParams.type,
                  mjChange: item.mj + '-' + mj,
                  title: item.title,
                  filesId: item.filesId
                }
              })
              approval(this.form).then(response => {
                this.$message({ message: '操作成功', type: 'success' })
                this.open = false
                this.getlistHeaderPost()
                setTimeout(() => {
                  this.noClick = true
                }, 300)
              }).catch(() => {
                setTimeout(() => {
                  this.noClick = true
                }, 300)
              })
            })
          }
        }
      })
    },
    closeDelDialog() {
      this.open = false
    },
    //双击列表进入详情页面
    celldblclick(row) {
      var level="";
      if (getLastIndexOf( this.queryParams.sub_table_name) == "F") {  //F 件
        level = 0
      } else if (getLastIndexOf(this.queryParams.sub_table_name) == "V") { //卷
        level = 1
      } else { //项目
        level = 3
      }
      this.celldbData = {
        id: row.ID,
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        sub_table_name: this.queryParams.sub_table_name,
        label: this.queryParams.label,
        level:level,
        ifSub_table_name:this.queryParams.sub_table_name,
        isDel:"0",  //判断是回收站数据还是接收收据 isDel:1  回收站    isDel:0 //离线接收、档案接收、在线接收
        tableHeaderList: this.tableHeaderList
      }
      this.$nextTick(() => {
        if (this.subStr == "F") {  //F 进入件详情
          this.jianDetailsOpen = true;
        } else { //进入卷详情 和项目详情
          this.juanDetailsOpen = true
        }
      })
    },
    //关闭详情页面
    detailsHandleClose() {
      this.jianDetailsOpen = false;
      this.juanDetailsOpen = false;
    },
  }
}
</script>

<style scoped>

</style>
